home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / DRVALID.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  4KB  |  160 lines

  1. /*
  2. **  DRVALID.C - validate disk drives
  3. **
  4. **  Original Copyright 1988-1991 by Bob Stout as part of
  5. **  the MicroFirm Function Library (MFL)
  6. **
  7. **  This subset version is functionally identical to the
  8. **  version originally published by the author in Tech Specialist
  9. **  magazine and is hereby donated to the public domain.
  10. */
  11.  
  12. #include <dos.h>
  13. #include <stdlib.h>
  14.  
  15. typedef enum {ERROR = -1, SUCCESS, FALSE = 0, TRUE} LOGICAL;
  16.  
  17. /*
  18. **  getdrv()
  19. **
  20. **  Just as getcwd() returns the default directory, getdrv() returns
  21. **  the current drive.
  22. **
  23. **  Arguments: None.
  24. **
  25. **  Returns:   Current drive (0 = A:, 1 = B:, etc.)
  26. **
  27. **  Side effects: none
  28. */
  29.  
  30. int getdrv(void)
  31. {
  32.       union REGS regs;
  33.  
  34.       regs.h.ah = 0x19;
  35.       intdos(®s, ®s);
  36.       return (regs.h.al);
  37. }
  38.  
  39. /*
  40. **  chdrv()
  41. **
  42. **  Like chdir(), except changes drives rather than directories.
  43. **
  44. **  Arguments: 1 - target drive (0 = A:, 1 = B:, etc.)
  45. **
  46. **  Returns: SUCCESS or ERROR
  47. **
  48. **  Side effects: none
  49. */
  50.  
  51. LOGICAL chdrv(int drive)
  52. {
  53.       union REGS regs;
  54.  
  55.       regs.h.ah = 0x0e;
  56.       regs.h.dl = (char)drive;
  57.       intdos(®s, ®s);
  58.       if (drive != getdrv())
  59.             return ERROR;
  60.       else  return SUCCESS;
  61. }
  62.  
  63. /*
  64. **  drvalid()
  65. **
  66. **  Verifies whether a logical disk drive is available without
  67. **  triggering the DOS critical error handler.
  68. **
  69. **  Arguments: 1 - target drive (0 = A;, 1 = B:, etc.)
  70. **
  71. **  Returns:   TRUE  - drive is valid
  72. **             FALSE - drive is invalid
  73. **
  74. **  Side effects: none
  75. */
  76.  
  77. LOGICAL drvalid(int drive)
  78. {
  79.       int original, result;
  80.  
  81.       original = getdrv();
  82.       result   = (SUCCESS == chdrv(drive));
  83.       chdrv(original);
  84.       return result;
  85. }
  86.  
  87. /*
  88. **  drvrdy()
  89. **
  90. **  Checks whether a drive with removable media is ready.
  91. **
  92. **  Arguments: 1 - target drive (0 = A;, 1 = B:, etc.)
  93. **
  94. **  Returns:   TRUE  - drive is ready
  95. **             FALSE - drive is not ready
  96. **             ERROR - other read error
  97. **
  98. **  Side effects: none
  99. */
  100.  
  101. LOGICAL drvrdy(int drive)
  102. {
  103.       int status;
  104.       char buf[2048];         /* nice & roomy   */
  105.  
  106.       status = AbsDiskRead(drive, 1, 0, buf);
  107.       printf("AbsDiskRead(%d, 1, 0) returned 0x%X\n", drive, status);
  108.       if (0 == status)
  109.             return TRUE;
  110.       status &= 0xff;
  111.       if (2 == status)
  112.             return FALSE;
  113.       else  return ERROR;
  114. }
  115.  
  116. #ifdef TEST
  117.  
  118. #include <stdio.h>
  119. #include <ctype.h>
  120.  
  121. int main(int argc, char *argv[])
  122. {
  123.       int drive;
  124.  
  125.       if (2 > argc)
  126.       {
  127.             puts("Usage: DRVALID drive[:]");
  128.             return EXIT_FAILURE;
  129.       }
  130.       drive = toupper(*argv[1]);
  131.       if (!isalpha(drive))
  132.       {
  133.             puts("Error: Invalid drive name");
  134.             return EXIT_FAILURE;
  135.       }
  136.       printf("Drive %c: is %svalid\n", drive,
  137.             drvalid(drive - 'A') ? "" : "not ");
  138.       if (2 < _osmajor)
  139.       {
  140.             union REGS regs;
  141.  
  142.             regs.x.ax = 0x4408;
  143.             regs.h.bl = (unsigned char)(drive - '@');
  144.             intdos(®s, ®s);
  145.             printf("ioctl returned Cflag=%s\n",
  146.                   regs.x.cflag ? "TRUE" : "FALSE");
  147.             printf("ioctl returned AX=0x%X\n", regs.x.ax);
  148.             printf("Drive %c is%s removable\n", drive,
  149.                   regs.x.ax ? " not" : "");
  150.             if (0 == regs.x.ax)
  151.             {
  152.                   printf("Drive %c is %sready\n", drive,
  153.                         drvrdy(drive - 'A') ? "" : "not ");
  154.             }
  155.       }
  156.       return EXIT_SUCCESS;
  157. }
  158.  
  159. #endif /* TEST */
  160.